package com.hiwan.system.web.dict;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.framework.dao.plugin.DataTablesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hiwan.platform.utils.StringUtils;
import com.hiwan.system.common.base.BaseController;
import com.hiwan.system.pojo.DictEntry;
import com.hiwan.system.pojo.DictType;
import com.hiwan.system.service.dict.DictService;
import com.hiwan.system.util.DictCacheUtils;


@Controller
@RequestMapping("/dict")
public class DictController extends BaseController{
	
	@Autowired
	private DictService dictService;
	
	@RequestMapping("frame.do")
	public String frame(){
		return "dict/frame";
	}
	
	@RequestMapping("toQuery.do")
	public String toQueryDict() {
		return "dict/queryDict";
	}
	
	
	@RequestMapping("addDict.do")
	public String addDict(ModelMap model,String dictType){
		DictType type = new DictType();
		if(dictType !=null&& dictType !=""){
			type.setDictType(dictType);
			type =dictService.selectDictType(type).get(0);
			model.put("sendFlag", "2");
		}else{
			model.put("sendFlag", "1");
		}
		
		model.put("sysDictType", type);
		return "dict/dictDetail";
	}
	
	
	@RequestMapping("save.do")
	public @ResponseBody String[] saveDict(DictType dictType){
		dictService.insertDictType(dictType);
		return new String[]{"ok","保存成功！"};
	}

	@RequestMapping("list.do")
	public String queryList(DictType dictType,ModelMap model){
		/*if(dictType == null){
			dictType = new SysDictType();
		}
		List<SysDictType> dictList = dictService.selectDictType(dictType);
		model.put("dictList", dictList);*/
		model.put("dictType", dictType);
		return "dict/dictList";
	}
	
	
	@RequestMapping("listData")
	@ResponseBody
	public Map<String, Object> queryListData(DictType dictType,ModelMap model){
		if(dictType == null){
			dictType = new DictType();
		}
		
		List<Object[]> list = new ArrayList<Object[]>();
		
		List<DictType> dictList = dictService.selectDictType(dictType);
		for (DictType type : dictList) {
			Object[] arr = new Object[4];
			arr[0] = type.getDictType();
			arr[1] = type.getDictDesc();
			arr[2] = type.getRemarks();
			arr[3] = DataTablesUtil.actionBtn(type.getDictType());
			list.add(arr);
		}
		return DataTablesUtil.buildDataTablesReturn(list);
	}
	
	
	
	@RequestMapping("entryList")
	public String entryList(String dictType,ModelMap model){
		List<DictEntry> entryList = DictCacheUtils.getDictList(dictType);
		model.put("entryList", entryList);
		model.put("dictType", dictType);
		return "dict/entryList";
	}
	
	@RequestMapping("dictTypeJson.do")
	@ResponseBody
	public Collection<DictType> dictTypeJson(){
		List<DictType> dictList = dictService.selectDictType(new DictType());
		return dictList;
	}
	
	
	@RequestMapping("entryDetail.do")
	public String entryDetail(String dictType,String orgId,String langCode,String dictCode,ModelMap model){
		DictEntry dictEntry = dictService.selectDictEntry(dictType,dictCode).get(0);
		model.put("sendFlag", "2");
		model.put("sysDictEntry", dictEntry);
		return "dict/entryDetail";
	}
	
	@RequestMapping("addEntry")
	public String addEntry(String dictType,String dictCode,ModelMap model){
		int dictOrder = dictService.selectDictEntry(dictType,dictCode).size();

		DictEntry dictEntry =new DictEntry();
		dictEntry.setDictType(dictType);
		dictEntry.setDictOrder(String.valueOf(dictOrder+1));
		model.put("sendFlag","1");
		model.put("sysDictEntry", dictEntry);
		return "dict/entryDetail";
	}
	@RequestMapping("saveEntry.do")
	public @ResponseBody String[] saveEntry(DictEntry dictEntry,HttpSession session){
		dictService.insertDictEntry(dictEntry);
		return new String[]{"ok","新增成功！"};
	}
	
	@RequestMapping("deleteEntry.do")
	public @ResponseBody String[] deleteEntry(String dictType,String orgId,String langCode,String dictCode,ModelMap model){
		dictService.deleteEntry(dictType,dictCode);
		return new String[]{"ok","删除成功！"};
	}
	@RequestMapping("updateEntry.do")
	public @ResponseBody String[] updateEntry(DictEntry dictEntry,HttpSession session){
		dictService.updateDictEntry(dictEntry);
		return new String[]{"ok","修改成功！"};
		
	}
	@RequestMapping("update.do")
	public @ResponseBody String[] updateDict(DictType dictType){
		String message = dictService.updateDictType(dictType);
		if("success".equals(message)){
			return new String []{"ok","修改成功！"};
		}else{
			return new String []{"fail","修改失败！"};
		}
	}
	
	@RequestMapping("checkDict.do")
	@ResponseBody
	public String checkDict(String dictType){
		return dictService.checkDictEntry(dictType);
	}
	
	@RequestMapping("removeDict.do")
	
	public @ResponseBody String[] removeDict(String dictType,HttpSession session){
		if(StringUtils.isBlank(dictType)){
			return new String[] { "fail", "删除失败！字典编码为空！" };
		}
		int a=dictService.removeDictType(dictType);
		if(a>0){
			return new String[] { "ok", "删除成功！" };
		}else{
			return new String[] { "fail", "删除失败，无权限操作！" };
		}
		
	}
}
